/* StringParser.java
 * 
 * Alfred Olschnoegger
 * 26.05.2008
 */

package application.parser;
import application.ServerInfoProvider;

import domain.GameObject;
import domain.Hunter;
import domain.game.Game;
import domain.location.Position;
import exceptions.RabbitBusterException;

/**
 * Parses the given field data into the specific string format.
 * Records are seperated by the s_RecordSeperator and the record fields are seperated by the
 * s_FieldSeperator.
 *
 * @date 26.05.2008
 * @author Alfred Olschnoegger
 */

public class StringParser extends Parser {	
	
	// internal parsed string
	private StringBuilder m_ExPars = new StringBuilder();
	private StringBuilder m_SuccParse = new StringBuilder();
	private StringBuilder m_DataParse = new StringBuilder();
	private StringBuilder m_InfoParse = new StringBuilder();
	private StringIdentifier m_Ident = StringIdentifier.getInstance();
	
	/**
	 * Returns the parsed information in the specific format.
	 * @return parsed information
	 */
	public String parse() {
		StringBuilder tmp = new StringBuilder();
		tmp.append(m_ExPars);
		tmp.append(m_SuccParse);
		tmp.append(m_DataParse);
		tmp.append(m_InfoParse);
		return tmp.toString();
	}

	/**
	 * Adds a GameObject to the Parsed String
	 * 
	 * @param obj object which should be added
	 */
	public void add(GameObject obj) {
		if(m_Ident.containsClassKey(obj.getClass().getSimpleName())) {
			if(m_DataParse.length() == 0)
				m_DataParse.append(m_Ident.getDataValue());
			
			m_DataParse.append(m_Ident.getClassIdent(obj.getClass().getSimpleName()));
						
			m_DataParse.append(m_Ident.getFieldSeperator());
			m_DataParse.append(obj.getID());
			m_DataParse.append(m_Ident.getFieldSeperator());
			m_DataParse.append(obj.getPos().getX());
			m_DataParse.append(m_Ident.getFieldSeperator());
			m_DataParse.append(obj.getPos().getY());
			m_DataParse.append(m_Ident.getRecordSeperator());
		}
	}

	/**
	 * adds a hunter to the parsed string
	 * 
	 * @param hunter hunter which should be added
	 */
	public void add(Hunter hunter) {
		if(m_DataParse.length() == 0)
			m_DataParse.append(m_Ident.getDataValue());
		m_DataParse.append(m_Ident.getClassIdent("Hunter"));
		m_DataParse.append(m_Ident.getFieldSeperator());
		m_DataParse.append(hunter.getName());
		m_DataParse.append(m_Ident.getFieldSeperator());
		m_DataParse.append(hunter.getKills());
		m_DataParse.append(m_Ident.getFieldSeperator());
		m_DataParse.append(hunter.getPos().getX());
		m_DataParse.append(m_Ident.getFieldSeperator());
		m_DataParse.append(hunter.getPos().getY());
		m_DataParse.append(m_Ident.getRecordSeperator());
		
	}

	/**
	 * 
	 * @param ex exception which should be added
	 */
	public void add(RabbitBusterException ex) {
		if(m_ExPars.length() == 0)
			m_ExPars.append(m_Ident.getExceptionValue());
		m_ExPars.append(m_Ident.getExceptionIdent(ex.getClass().getSimpleName()));
		m_ExPars.append(m_Ident.getRecordSeperator());

	}

	/**
	 * @param authKey authkey which should be added
	 */	
	public void addAuth(long authKey) {
		if(m_InfoParse.length() == 0)
			m_InfoParse.append(m_Ident.getInformationValue());
		m_InfoParse.append(m_Ident.getClassIdent("AuthKey"));
		m_InfoParse.append(m_Ident.getFieldSeperator());
		m_InfoParse.append(authKey);
		m_InfoParse.append(m_Ident.getRecordSeperator());
	}

	/**
	 * adds the success code to the string
	 */
	public void success() {
		if(m_SuccParse.length() == 0)
			m_SuccParse.append(m_Ident.getSuccessValue());
	}

	/**
	 * appends the GameInfo to the parse string
	 * g,<ID>,<Name>,xLat,xLong,yLat,yLong
	 */
	public void add(Game g) {
		Position tmpPos;
		if(m_DataParse.length() == 0)
			m_DataParse.append(m_Ident.getDataValue());
		
		
		m_DataParse.append(m_Ident.getClassIdent(g.getClass().getSimpleName()));
		m_DataParse.append(m_Ident.getFieldSeperator());
		
		m_DataParse.append(g.getGameID());
		m_DataParse.append(m_Ident.getFieldSeperator());
		
		m_DataParse.append(g.getGameName());
		m_DataParse.append(m_Ident.getFieldSeperator());
		
		m_DataParse.append(g.getRemainingTimeslice());
		m_DataParse.append(m_Ident.getFieldSeperator());
		
		tmpPos = g.getArea().getX();
		m_DataParse.append(tmpPos.getGpsX());
		m_DataParse.append(m_Ident.getFieldSeperator());
		m_DataParse.append(tmpPos.getGpsY());
		m_DataParse.append(m_Ident.getFieldSeperator());
		
		tmpPos = g.getArea().getY();
		m_DataParse.append(tmpPos.getGpsX());
		m_DataParse.append(m_Ident.getFieldSeperator());
		m_DataParse.append(tmpPos.getGpsY());
		
		
		m_DataParse.append(m_Ident.getRecordSeperator());
	}
	

	@Override
	public void add(ServerInfoProvider info) {
		if(m_InfoParse.length() == 0)
			m_InfoParse.append(m_Ident.getInformationValue());
		m_InfoParse.append(m_Ident.getClassIdent(info.getClass().getSimpleName()));
		m_InfoParse.append(m_Ident.getFieldSeperator());
		m_InfoParse.append(info.getVersion());
		m_InfoParse.append(m_Ident.getRecordSeperator());
	}	
}


// end of StringParser.java
